﻿#include "PointGraphicsItem.h"
#include "GraphicsDef.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QFocusEvent>

namespace OfUI {

	PointGraphicsItem::PointGraphicsItem(QGraphicsItem *parent)
		: BaseGraphicsItem(parent)
		, m_bAdding(true)
		, m_eSelType(SELECT_NONE)
	{
		setType(ItemType::POINT);
	}

	PointGraphicsItem::~PointGraphicsItem()
	{}

	void PointGraphicsItem::setPoint(const QPointF& point)
	{
		prepareGeometryChange();
		m_point = point;
		emit signalDataChanged();
	}

	QPointF PointGraphicsItem::getPoint() const
	{
		return m_point;
	}

	bool PointGraphicsItem::isAdding() const
	{
		return m_bAdding;
	}

	bool PointGraphicsItem::isEditing() const
	{
		return SELECT_NONE != m_eSelType;
	}

	bool PointGraphicsItem::isVaild() const
	{
		return !m_point.isNull();
	}

	BaseGraphicsItem::MouseDragType PointGraphicsItem::select(const QPointF& point, int* selType /*= nullptr*/) const
	{
		MouseDragType eDragType = DRAG_NONE;
		SelectType eSelectType = SELECT_NONE;

		double dDistance = 4 / getViewScale();

		if (QLineF(m_point, point).length() <= dDistance)
		{
			eDragType = DRAG_EDIT;
			eSelectType = SELECT_POINT;
		}
		if (selType)
		{
			*selType = eSelectType;
		}
		return eDragType;
	}

	void PointGraphicsItem::cursor(const QPointF& point, QCursor& cursor) const
	{
		if (BaseGraphicsItem::DRAG_NONE != select(point))
		{
			cursor = Qt::SizeAllCursor;
		}
	}

	void PointGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
	{
		if (Qt::LeftButton == event->button())
		{
			if (isAdding())
			{
				setPoint(event->scenePos());
				m_eSelType = SELECT_POINT;
			}
			else
			{
				int nType = SELECT_NONE;
				if (BaseGraphicsItem::DRAG_NONE != select(event->scenePos(), &nType))
				{
					m_eSelType = static_cast<SelectType>(nType);
					return;
				}
			}
		}
		QFocusEvent focusEvent(QEvent::FocusOut, Qt::OtherFocusReason);
		focusOutEvent(&focusEvent);
	}

	void PointGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
	{
		QFocusEvent focusEvent(QEvent::FocusOut, Qt::OtherFocusReason);
		focusOutEvent(&focusEvent);
	}

	void PointGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
	{
		if (SELECT_NONE == m_eSelType)
			return;

		QPointF mousePos = event->scenePos();
		QRectF sceneRect = scene()->sceneRect();
		// 保证鼠标在场景内
		mousePos.setX(qMin(mousePos.rx(), sceneRect.right()));
		mousePos.setY(qMin(mousePos.ry(), sceneRect.bottom()));
		mousePos.setX(qMax(mousePos.rx(), sceneRect.left()));
		mousePos.setY(qMax(mousePos.ry(), sceneRect.top()));

		setPoint(mousePos);
	}

	void PointGraphicsItem::focusOutEvent(QFocusEvent *event)
	{
		Q_UNUSED(event);
		if (isAdding())
		{
			emit signalAddFinished();
			m_bAdding = false;
		}
		m_eSelType = SELECT_NONE;
	}

	QRectF PointGraphicsItem::boundingRect() const
	{
		QPainterPath path;
		double dRadius = 4 / getViewScale();
		path.addEllipse(m_point, dRadius, dRadius);

		QRectF rectF = path.controlPointRect().adjusted(-5, -5, 5, 5);
		return rectF;
	}

	void PointGraphicsItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
	{
		Q_UNUSED(option);
		Q_UNUSED(widget);

		if (this->scene())
		{
			painter->save();
			painter->setRenderHint(QPainter::Antialiasing);

			// 通过反向缩放painter实现矢量绘制效果
			double dViewScale = getViewScale();
			painter->scale(1 / dViewScale, 1 / dViewScale);

			// painter缩小，则绘制坐标放大，保证显示位置不变
			QPointF point = m_point * dViewScale;

			// 绘制点
			double dRadius = 4;
			painter->setPen(getPen(getState()));
			painter->setBrush(painter->pen().color());
			painter->drawEllipse(point, dRadius, dRadius);
			painter->restore();
		}
	}

}
